﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace easyExcel.Util
{
    class ExcelEditUtil
    {
        public static void AddComment(Excel.Range range, string str)
        {

            string strOldText = string.Empty;

            int len;
            len = str.Length;
            //range.Comment.Previous();
            //strOldText = range.Comment.ToString();
            str = Environment.UserName + " " + DateTime.Now.ToString() + Environment.NewLine + str + Environment.NewLine;
 

            if (range.Comment == null)
            {
                range.AddComment(str);
            }
            else
            {
                strOldText = range.Comment.Text();
                range.Comment.Text(str + Environment.NewLine + strOldText);
            }

            range.Comment.Shape.TextFrame.Characters(1, str.Length).Font.ColorIndex = 3;
            range.Comment.Shape.TextFrame.Characters(str.Length - len - 1, str.Length).Font.Bold = false;
            range.Comment.Shape.TextFrame.Characters(str.Length + 1).Font.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
            range.Comment.Shape.TextFrame.Characters(str.Length + 1).Font.Bold = false;
            range.Comment.Shape.TextFrame.AutoSize = true;

        }

        public static void DeleteComment(Excel.Range range, string str)
        {
            if (range.Comment != null)
            {
                range.Comment.Delete();    
            }
            
        }
    }
}
